# -*- coding: utf-8 -*-
"""
# 使用了 [gensim==4.3.3]，遵循其 [LGPL-2.1-only] 许可证，原始代码来源：[https://radimrehurek.com/gensim/]
# 使用了 [numpy==1.24.4]，遵循其 [BSD-3-Clause] 许可证，原始代码来源：[https://www.numpy.org]
# 使用了 [pandas==2.0.3]，遵循其 [BSD 3-Clause License] 许可证，原始代码来源：[https://pandas.pydata.org]
"""
import os,sys
from pathlib import Path
import numpy as np
import pandas as pd
from gensim.models import LdaMulticore
from gensim.matutils import Sparse2Corpus
class PARA_CONFIG:
    CURRENT_DIR=Path(__file__).parent.resolve()
    TFIDF_PATH=CURRENT_DIR.parent.parent.resolve()/"01_prepare_data"/"src"/"result"/"1_ICT_TFIDF_make_20251031_111556.xlsx"
    FUNC_PATH=CURRENT_DIR.parent.parent.resolve()/"about_file"
sys.path.append(str(PARA_CONFIG.FUNC_PATH))
import f_basic

@f_basic.Timer
def do_lda_analyse(fp,n_topics=0):
    (titles,cv,li_keys)=f_basic.make_csr_matrix(fp)#make csr.
    cnt_paper=len(titles)
    if n_topics==0:
        N_TOPICS=f_basic.sqrt_ceil(cnt_paper)
    else:
        N_TOPICS=n_topics
    corpus=Sparse2Corpus(cv,documents_columns=False)
    id2word=dict(enumerate(li_keys))
    workers=max(1,os.cpu_count()-1)
    chunksize=min(2000, max(100,cnt_paper//10))
    lda=LdaMulticore(corpus=corpus,id2word=id2word,num_topics=N_TOPICS,passes=3,iterations=100,chunksize=chunksize,workers=workers,random_state=0,alpha='symmetric',eta='auto',dtype=np.float32,minimum_probability=0.01)
    doc_topics=np.zeros((cnt_paper,N_TOPICS),dtype=np.float32)
    for i,doc in enumerate(lda[corpus]):
        for topic_id,prob in doc:
            if prob>=0.01:
                doc_topics[i,topic_id]=prob
    topic_word = lda.get_topics()
    df_topic = pd.DataFrame(doc_topics,columns=[f"{i}" for i in range(N_TOPICS)],index=titles,dtype=np.float32)
    df_components = pd.DataFrame(topic_word,columns=li_keys)
    fp_topics = f_basic.save_dataframe(df_topic, "2_Gensim_Topics")
    fp_keywords = f_basic.save_dataframe(df_components, "3_Gensim_Keywords")
    return (fp_topics,fp_keywords)

@f_basic.Timer 
def main_stream():
    """GS LDA start."""
    (fp_t,fp_k)=do_lda_analyse(PARA_CONFIG.TFIDF_PATH)
    f_basic.print_topic_keys(fp_k)
    f_basic.draw_tsne_graph(fp_t,prefix="1_LDA")

if __name__=="__main__":
    main_stream()
